package com.sxt.generate.configuration;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @ClassName: Configuration
 * @Description:Configuration
 * @version:v1.0.0
 * @author: haning
 * @date:
 */
public class ConfigurationParser {
    public static  void parse() {
        SAXReader reader = new SAXReader();
        try {
            Document root = reader.read(ConfigurationParser.class.getResourceAsStream("/generator.xml"));
            List<Element> templates = root.selectNodes("/configuration/template/property");
            for(Element property :templates) {
                Configuration.getTemplate().put(property.attribute("name").getValue(), property.attribute("value").getValue());
            }
            List<Element> jdbcs = root.selectNodes("/configuration/jdbc/property");
            for(Element property :jdbcs) {
                Configuration.getJdbc().put(property.attribute("name").getValue(), property.attribute("value").getValue());
            }

            List<Element> sources = root.selectNodes("/configuration/sources/source");
            for(Element property :sources) {
                if(property.attribute("name")!=null &&  property.attribute("module") !=null) {
                    Configuration.getSource().put(property.attribute("name").getValue()+ ".module", property.attribute("module").getValue());
                }
                if(property.attribute("name")!=null &&  property.attribute("basePackage") !=null) {
                    Configuration.getSource().put(property.attribute("name").getValue()+ ".basePackage", property.attribute("basePackage").getValue());
                }
            }

            List<Element> resources = root.selectNodes("/configuration/resources/resource");
            for(Element property :resources) {
                if(property.attribute("name")!=null &&  property.attribute("module") !=null) {
                    Configuration.getResource().put(property.attribute("name").getValue()+ ".module", property.attribute("module").getValue());
                }
                if(property.attribute("name")!=null &&  property.attribute("basePackage") !=null) {
                    Configuration.getResource().put(property.attribute("name").getValue()+ ".basePackage", property.attribute("basePackage").getValue());
                }
            }


            List<Element> generates = root.selectNodes("/configuration/generates/generate");
            for(Element property :generates) {
                Map generateMap = new HashMap<String, String>();
                if(property.element("tableName") != null) {
                    generateMap.put("tableName",property.element("tableName").getText().trim() );
                }
                if(property.element("className") != null) {
                    generateMap.put("className",property.element("className").getText().trim() );
                }
                if(property.element("basePackage") != null) {
                    generateMap.put("basePackage",property.element("basePackage").getText().trim() );
                }
                if(property.element("override") != null) {
                    generateMap.put("override",property.element("override").getText().trim() );
                }
                if(property.element("junit") != null) {
                    generateMap.put("junit",property.element("junit").getText().trim() );
                }
                Configuration.getGenerates().add(generateMap);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }


}
